package me.imli.app.ui;

import me.imli.app.R;
import me.imli.app.model.RssItemModel;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

@EActivity(R.layout.activity_detail)
public class DetailActivity extends BaseActivity {
	
	public static final String INTENT_DATA = "inten_data";
	
	private static final String HTML_HEAD = "<html> <head> <meta charset=\"utf-8\"> <meta name=\"viewport\"content=\"width=device-width,initial-scale=1.0\"> <link href=\"http://www.imli.me/assets/themes/twitter/bootstrap/bootstrap/css/bootstrap.2.2.2.min.css\"rel=\"stylesheet\"> <link href=\"http://www.imli.me/assets/themes/twitter/bootstrap/css/style.css?body=1\"rel=\"stylesheet\"type=\"text/css\"media=\"all\"> <link href=\"http://www.imli.me/assets/themes/twitter/bootstrap/css/prettify.css\"rel=\"stylesheet\"type=\"text/css\"> <script src=\"http://www.imli.me/assets/themes/twitter/bootstrap/js/prettify.js\"></script> </head> <body>";
	private static final String HTML_FOOT = "</body><footer class=\"card clearfix\"><span style=\"font-size:30px;\"class=\"fa fa-github pull-left\"></span><a href=\"https://github.com/iquick\" style=\"display:inline-block;padding-right:80px;border-right:1px solid #bbb\">Fork Me On GitHub</a><span style=\"font-size:30px;\"class=\"fa fa-github pull-left\"></span><a href=\"http://www.imli.me\" style=\"display:inline-block;padding-right:80px;border-right:1px solid #bbb\">My Blog</a></footer></div></body></html>";
	
	@ViewById(R.id.swl_detail)
	SwipeRefreshLayout mRefreshLayout;
	
	@ViewById(R.id.web_detail)
	WebView mWebView;
	
	private String mHtml;

	@AfterViews
	void init() {
		RssItemModel model = (RssItemModel) getIntent().getSerializableExtra(INTENT_DATA);
		mRefreshLayout.setOnRefreshListener(onRefreshListener());
		
		Log.i(TAG, model.description);
		mHtml = HTML_HEAD + model.description + HTML_FOOT;
		
		mRefreshLayout.setRefreshing(true);
		WebSettings mWebSettings = mWebView.getSettings();
		mWebSettings.setJavaScriptEnabled(true);
		mWebSettings.setDomStorageEnabled(true);
		mWebSettings.setDefaultTextEncodingName("UTF-8"); 
		mWebView.setWebViewClient(new MyWebViewClient());
		mWebView.setWebChromeClient(new MyWebChromeClient());
		mWebView.loadDataWithBaseURL(model.link, mHtml, "text/html", "utf-8", null);
	}
	
	private SwipeRefreshLayout.OnRefreshListener onRefreshListener() {
		return new SwipeRefreshLayout.OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				mWebView.loadDataWithBaseURL(null, mHtml, "text/html", "utf-8", null);
			}
		};
	}
	
	
	/**
	 * MyWebViewClient
	 * @author Doots
	 *
	 */
	private class MyWebViewClient extends WebViewClient {
		
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			// TODO Auto-generated method stub
			if(url.indexOf("tel:")<0){//页面上有数字会导致连接电话  
				Intent intent = new Intent(getActivity(), WebActivity_.class);
				intent.putExtra(WebActivity.INTENT_URL, url);
				startActivity(intent);
			}
			return true;
		}
		
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			mRefreshLayout.setRefreshing(true);
			super.onPageStarted(view, url, favicon);
		}
		
		@Override
		public void onPageFinished(WebView view, String url) {
			mRefreshLayout.setRefreshing(false);
			super.onPageFinished(view, url);
		}
		
		@Override
		public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
			mRefreshLayout.setRefreshing(false);
			super.onReceivedError(view, errorCode, description, failingUrl);
		}
	}
	
	/**
	 * MyWebChromeClient
	 * @author Doots
	 *
	 */
	private class MyWebChromeClient extends WebChromeClient {
		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			if (newProgress < 100 && newProgress >= 0) {
			} else if (newProgress >= 100) {
			}
		}
	}
}
